package view;

import java.awt.Point;
import java.util.List;

import controller.Movement;


/**
 * Class used to manage the game display- displaying the board and interacting with the user
 */
public class GuiViewController implements ViewController {

	private ChessGUI guiManager; // manages game GUI display 
	private InputOutputMngr ioManager; // manages input and output from user

	/**
	 * Constructor- creates a new display controller
	 * @param guiManager- object to manage game GUI display
	 */
	public GuiViewController(ChessGUI guiManager) {
		this.guiManager = guiManager;
		
		ioManager = new ContactUser();
	}
	
	/**
	 * Waits until user has chosen a move.
	 * Gets the move from the GUI and returns it
	 */
	@Override
	public Movement getMovement() {
		
		// enable board
		guiManager.setEnable(true);
		
		// wait for user choice
		while (!guiManager.isHumanMovementDone()) {
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// use has chose, disable board
		guiManager.setEnable(false);
		
		// initialize move-choice indicator
		guiManager.humanMovementDone.set(false);
		
		// return movement chosen by user
		return guiManager.selectedMove;
	}
		
	/**
	 * Uses the GUI manager to display the game board
	 */
	@Override
	public void displayBoard() {
		guiManager.displayBoard();
	}
	
	/**
	 * Adds the piece adding panel to the GUI
	 */
	@Override
	public void setPiecesFromUser() {
		
		// disable display
		guiManager.setEnable(false);
		
		// place panel for adding pieces on game display
		guiManager.setBoardCustom();
	}
	
	/**
	 * Uses the input-output manager to notify user that game is in Check
	 */
	public void notifyCheck() {
		ioManager.notifyCheck();
	}
	
	/**
	 * Uses the input-output manager to notify user that game is over, and disables the GUI
	 */
	public void notifyGameOver(String msg) {
		ioManager.notifyGameOver(msg);
		guiManager.setEnable(false);
	}
	
	/**
	 * Uses the input-output manager to ask user for piece to replace Pawn 
	 */
	public int replacePawn(String[] options, int defaultOption) {
		return ioManager.notifyReplacePawn(options, defaultOption);
	}
	
	/**
	 * Initializes the display by removing the piece-adding panel from the GUI
	 */
	public void initializeDisplay() {
		guiManager.removePiecePanel();
	}
	
	/**
	 * Refreshes Creatures' positions and positions where two Creatures were collided 
	 */
	public void refreshDisplayBeforeMove(List<Point> creaturePositions, List<Point> collidingCreatures) {
		guiManager.refreshDisplayBeforeMove(creaturePositions, collidingCreatures);
	}
	
	/**
	 * Refreshes the board display according to move made, Creature positions, colliding Creatures and Creature that killed a piece
	 */
	public void refreshDisplayAfterMove(Movement movement, Point killerCreature) {
		guiManager.refreshDisplayAfterMove(movement, killerCreature);
	}
	
	/**
	 * Uses the GUI manager to clean the collided and killed Creatures from the game display
	 */
	public void cleanKilledCreatures(List<Point> collidedCreatures, List<Point> killedCreatures) {
		guiManager.cleanKilledCreatures(collidedCreatures, killedCreatures);
	}
}